package cn.itcast.bos.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.CourierService;
import cn.itcast.bos.web.action.common.BaseAction;
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class CourierAction extends BaseAction<Courier> {

	private static final long serialVersionUID = 1L;
	
	
	//接收作废快递员信息id数组字符串
	private String courierArr;
	public void setCourierArr(String courierArr) {
		this.courierArr = courierArr;
	}

	@Autowired
	private CourierService courierService;

	//快递员信息保存
	@Action(value="courier_save",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String save(){
		courierService.save(model);
		return SUCCESS;
	}

	//快递员分页显示
	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		/*Pageable pageable = new PageRequest(page-1, rows);
		Page<Courier> page= courierService.findAll(pageable);
		Map<String,Object> map = new HashMap<>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		ActionContext.getContext().getValueStack().push(map);*/
		
		Pageable pageable = new PageRequest(page-1, rows);
		
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				List<Predicate> list = new ArrayList<>();
				
				if(StringUtils.isNotBlank(model.getCourierNum())){
					Predicate p = cb.equal(root.get("courierNum").as(String.class),model.getCourierNum() );
					list.add(p);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate p = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p);
				}
				if(StringUtils.isNotBlank(model.getType())){
					Predicate p = cb.like(root.get("type").as(String.class),"%"+ model.getType()+"%");
					list.add(p);
				}
				Join<Courier, Standard> join = root.join("standard", JoinType.INNER);
				if(model.getStandard() != null && model.getStandard().getId()!=null){
					Predicate p = cb.equal(join.get("id").as(Integer.class), model.getStandard().getId());
					list.add(p);
				}
				//将list转换成 predicate对象
				if(list != null && list.size() != 0){
					Predicate[] pArr = new Predicate[list.size()];
					predicate = cb.and(list.toArray(pArr));
					
				}
				return predicate;
			}
		};
		
		Page<Courier> page = courierService.findPageQueryCourierByCondition(specification,pageable);
		pushPageToValueStack(page);
		
		return SUCCESS;
	}
	
	
	/**快递员信息作废*/
	@Action(value="courier_delete",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String delete(){
		String[] courierArr2 = courierArr.split(",");
		
		courierService.delete(courierArr2);
		
		return SUCCESS;
	}
	
	/**快递员信息还原功能*/
	@Action(value="courier_restore",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String restore(){
		String [] courierArr2 = courierArr.split(",");
		courierService.restore(courierArr2);
		return SUCCESS;
	}
	
	//定区关联快递员
	@Action(value="courier_findnoassociation",results={@Result(name="success",type="json")})
	public String findnoassociation(){
		List<Courier> list = courierService.findnoassociation();
		
		
		ActionContext.getContext().getValueStack().push(list);
		
		return SUCCESS;
	}
	//接收定区id
	private String fixedAreaId;
	public void setFixedAreaId(String fixedAreaId) {
		this.fixedAreaId = fixedAreaId;
	}

	//双击,底部回显快递员信息
	@Action(value="courier_findAssociationCourier",results={@Result(name="success",type="json")})
	public String findAssociationCourier(){
		List<Courier> list = courierService.findAssociationCourier(fixedAreaId);
		ActionContext.getContext().getValueStack().push(list);
		
		return SUCCESS;
	}
	
	
	
}
